package com.erastep.mvc.mediator
{
	import com.erastep.mvc.EraStepFrame;
	import com.erastep.mvc.ifc.IMedator;
	import com.erastep.mvc.ifc.IProxy;
	import com.erastep.mvc.module.SimpleModule;
	
	import mx.controls.Alert;

	/**
	 * @date 2011/4/1
	 * @author Jarvis
	 * @depart NB
	 * */
	public class Mediator implements IMedator
	{
		// the mediator name
		protected var mediatorName:String;
		
		// The view component
		protected var viewComponent:Object;
		
		public var mg:SimpleModule;
		
		private var _host:String;
		
		public static const NAME:String = "Mediator";
		
		public function Mediator(mediatorName:String = null,viewComponent:Object = null)
		{
			this.mediatorName = (mediatorName != null)?mediatorName:NAME; 
			this.viewComponent = viewComponent;	
		}
		
	    public function sendNotification(notificationName:String, body:Object=null, type:String=null, toId:String=null, fromId:String=null):void{
			if(mg){
				mg.sendNotification(notificationName,body,type,toId,fromId);
			}else{
				EraStepFrame.getInstance().sendNotification(notificationName,body,type,toId,fromId);
			}
		}
		/**
		 * 注册mediator
		 * */
		public function registerMediator(mediator:IMedator):void{
			if(mg){
				mg.registerMediator(mediator);
			}else{
				EraStepFrame.getInstance().registerMediator(mediator);
			}
		}
		/**
		 * 移除mediator
		 * */
		public function removeMediator(mediatorName:String):void{
			if(mg){
				mg.removeMediator(mediatorName);
			}else{
				EraStepFrame.getInstance().removeMediator(mediatorName);
			}
		}
		/**
		 * 实例mediator
		 * */
		public function retriveMediator(mediatorName:String):IMedator{
			var imediator:IMedator = null;
			if(mg){
				imediator = mg.retriveMediator(mediatorName);
			}else{
				imediator = EraStepFrame.getInstance().retriveMediator(mediatorName);
			}
			return imediator;
		}
		
		/**
		 * 注册mediator
		 * */
		public function registerProxy(proxy:IProxy):void{
			if(mg){
				mg.registerProxy(proxy);
			}else{
				EraStepFrame.getInstance().registerProxy(proxy);
			}
		}
		
		/**
		 * 移除mediator
		 * */
		public function removeProxy(proxyName:String):void{
			if(mg){
				mg.removeProxy(proxyName);
			}else{
				EraStepFrame.getInstance().removeProxy(proxyName);
			}
		}
		
		/**
		 * 实例mediator
		 * */
		public function retriveProxy(proxyName:String):IProxy{
			var iproxy:IProxy = null;
			if(mg){
				iproxy = mg.retriveProxy(proxyName);
			}else{
				iproxy = EraStepFrame.getInstance().retriveProxy(proxyName);
			}
			return iproxy;
		}
		
		/**
		 * 在注册Mediator之后,被
		 * */
		public function onRegister():void{
			
		}
		
		public function getMediatorName():String{
			return this.mediatorName;
		}
		
		public function onRemove():void{
		
		}
		
		public function set MessageManager(mg:SimpleModule):void{
			this.mg = mg;
		}
		
		public function set host(host:String):void{
			this._host = host;
		}
		public function get host():String{
			return this._host;
		}
		
		/**
		 * 快速执行方法
		 * */
		public function callFunction(functionName:String,param:Object = null):Object{
			if(this.hasOwnProperty(functionName)){
				var fun:Function = this[functionName] as Function;
				if(fun != null){
					return fun.call(null,param);
				}else{
					Alert.show("请确认 "+functionName +" 为方法名!");
					return null;
				}
			}else{
				Alert.show("此类中未找到方法名为 "+functionName);
			}
			return null;
		}
		
		
	}
}